<?php
	session_start();	
?>

<?php
	if( $_SESSION["admin"] != "" )
	{
?>
<script type="text/javascript" language="javascript">
function validateForm()
{
  var valid="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
  var formObj;
  formObj = document.updatecategory;
  if (formObj.admin_title.value == "") {
    alert("You have not filled in category name.");
    formObj.CouponCode.focus();
    return false;
  }
    if (formObj.parent.value == "") {
    alert("You have not filled in parent.");
    formObj.CouponTitle.focus();
    return false;
  }
   if (formObj.left.value == "") {
    alert("You have not filled in left.");
    formObj.CouponTitle.focus();
    return false;
  }
   if (formObj.right.value == "") {
    alert("You have not filled in right.");
    formObj.CouponTitle.focus();
    return false;
  }
    
return true;
}
</script>
<a href="index.php?com=category"><b>List category</a></b> | <b><a href="index.php?com=category&func=add">Add new category</b></a>
<br />
<?php
include_once("process/category.php");
    include_once("process/template.php");
	$category = new category();
    $template = new template();
	$path = rtrim($_SERVER['PHP_SELF'],"index.php")."/library/fckeditor/";
    
 	$value = $template->GetList();
	$select = '';
	$select = '<select name="template_id">';
	foreach($value as $k => $v)
	{
		if($v['id'] == $row['template_id'])
		{
			$select .= '<option selected value="'.$v['id'].'">'.$v['template_name'].'</option>';
		}
		else
		{
			$select .= '<option value="'.$v['id'].'">'.$v['template_name'].'</option>';
		}
	}
	$select .= '</select>';
	
	if( $_GET["com"] == "category" && $_GET["func"] == "update" && isset($_GET["id"]))
	{
		$row = $category->GetOneCategory($_GET['id']);	
		$path = rtrim($_SERVER['PHP_SELF'],"index.php")."/library/fckeditor/";
	}elseif( isset($_POST['btnUpdate']) )
	{	
		$file = $_FILES["main_image"];
		$file_name = $file["name"];
		$file_ext = explode(".",$file_name);
		if($file["name"] == "")
		{			
			$admin_title = stripcslashes($_POST["admin_title"]);
            $parent = stripcslashes($_POST["parent"]);
            //$left = stripcslashes($_POST["left"]);
            //$right = stripcslashes($_POST["right"]);
            $status = stripcslashes($_POST["status"]);
            $main_image = $file["name"];
            $short_description=stripcslashes($_POST["short_description"]);
			$description=stripcslashes($_POST["description"]);
            $main_menu=stripcslashes($_POST["main_menu"]);
            $template_id=stripcslashes($_POST["template_id"]);
            $keywords=stripcslashes($_POST["description"]);
            $tag_description=stripcslashes($_POST["description"]);
            $friendly_url=stripcslashes($_POST["friendly_url"]);
            $SortPriority=stripcslashes($_POST["SortPriority"]);
            $FeaturedCategory=stripcslashes($_POST["FeaturedCategory"]);
            $CatFomProduct=stripcslashes($_POST["description"]);
            $photo_link=stripcslashes($_POST["photo_link"]);
			$embed_youtube=stripcslashes($_POST["embed_youtube"]);
            $NaturalSearchDescription=stripcslashes($_POST["description"]);
            $id = $_POST["category_id"];
            
            if($admin_title!= "" && $parent != "" && $left != "" && $right != "")
            {
			$category->UpdateCategory($admin_title, $parent, $status, $main_image, $short_description, 
            $description, $main_menu, $template_id, $keywords, $tag_description, $friendly_url, $SortPriority, 
            $FeaturedCategory, $CatFomProduct, $photo_link, $embed_youtube, $NaturalSearchDescription, $id);
						
			echo '<meta http-equiv="Refresh" content="0;URL=index.php?com=category" />';
			return;
            }
		}

		if($file["name"] != "" && ($file_ext[count($file_ext)-1] == "jpg" || $file_ext[count($file_ext)-1] == "jpeg" || $file_ext[count($file_ext)-1] == "gif"))
		{										
			
			$admin_title = stripcslashes($_POST["admin_title"]);
            $parent = stripcslashes($_POST["parent"]);
            //$left = stripcslashes($_POST["left"]);
            //$right = stripcslashes($_POST["right"]);
            $status = stripcslashes($_POST["status"]);
            $main_image = $file["name"];
            $short_description=stripcslashes($_POST["short_description"]);
			$description=stripcslashes($_POST["description"]);
            $main_menu=stripcslashes($_POST["main_menu"]);
            $template_id=stripcslashes($_POST["template_id"]);
            $keywords=stripcslashes($_POST["description"]);
            $tag_description=stripcslashes($_POST["description"]);
            $friendly_url=stripcslashes($_POST["friendly_url"]);
            $SortPriority=stripcslashes($_POST["SortPriority"]);
            $FeaturedCategory=stripcslashes($_POST["FeaturedCategory"]);
            $CatFomProduct=stripcslashes($_POST["description"]);
            $photo_link=stripcslashes($_POST["photo_link"]);
			$embed_youtube=stripcslashes($_POST["embed_youtube"]);
            $NaturalSearchDescription=stripcslashes($_POST["description"]);
            $id = $_POST["category_id"];
         
             if($admin_title!= "" && $parent != "" && $left != "" && $right != "")
            {
			$category->UpdateCategory($admin_title, $parent, $status, $main_image, 
            $short_description, $description, $main_menu, $template_id, $keywords, $tag_description, 
            $friendly_url, $SortPriority, $FeaturedCategory, $CatFomProduct, $photo_link, $embed_youtube,
             $NaturalSearchDescription, $id);
					
			
			move_uploaded_file($_FILES["main_image"]["tmp_name"],"../asset/".$_FILES["main_image"]["name"]);
			
			echo '<meta http-equiv="Refresh" content="0;URL=index.php?com=category" />';
			return;
            }
		}
		else if($file["name"] != "" && ($file_ext[count($file_ext)-1] != "jpg" && $file_ext[count($file_ext)-1] != "jpeg" && $file_ext[count($file_ext)-1] != "gif"))
		{
			$alert='<script type="text/javascript">
			alert("You are only allowed to select the file format jpg, jpeg, gif");
			</script>';
			echo $alert;
		}
	}
	else
	{
		echo '<meta http-equiv="Refresh" content="0;URL=index.php?com=product" />';
	}	
	
?>
<form method="post" action="index.php?com=category&func=update" id="updatecategory" name="updatecategory" onSubmit="return validateForm()">
	<table width="90%" style="font-size:10pt;">
		<tr>
			<td align="center" colspan="4" height="50"><b style="font-size:12pt; color:Blue;">Update category</b></td>
		</tr>
		<tr>
			<td align="right">Category name :&nbsp;&nbsp;</td>
			<td>
				<input type="text" name="admin_title" id="admin_title" value="<?php echo $row['admin_title']; ?>" /><font style="color: red;"> *</font>
                <input type="hidden" name="category_id" id="category_id" value="<?php echo $row['category_id'] ?>" />
			</td>
		</tr>
        <tr>
			<td align="right">Parent :&nbsp;&nbsp;</td>
			<td>
				<input type="text" name="parent" id="parent" value="<?php echo $row['parent']; ?>"/><font style="color: red;"> *</font>
			</td>
		</tr>
        <!--</tr>
        	<tr>
			<td align="right">Left :&nbsp;&nbsp;</td>
			<td>
				<input type="text" name="left" id="left" value="<?php //echo $row['left']; ?>" /><font style="color: red;"> *</font>
			</td>
		</tr>
        </tr>
        	<tr>
			<td align="right">Right :&nbsp;&nbsp;</td>
			<td>
				<input type="text" name="right" id="right" value="<?php //echo $row['right']; ?>"/><font style="color: red;"> *</font>
			</td>
		</tr>-->
        <tr>
			<td valign="top" width="180" align="right">Status</td>
			<td valign="top" width="620"><select size="1" name="status">
            <?php
                if($row['status'] == 1)
                {
            ?>
                <option value="1">Enable</option>
                <option value="0">Disible</option>
            <?php
                }
                else
                {
            ?>
                <option value="0">Disible</option>
                <option value="1">Enable</option>
            <?php
                }
            ?>
			
			</select></td>
		</tr>
         <tr>
			<td align="right">Image :&nbsp;&nbsp;</td>
			<td>
                <input type="file" name="main_image" id="main_image" />
			</td>
		</tr>
        <tr>
			<td align="right">Sort description :&nbsp;&nbsp;</td>
			<td>
                <textarea rows="7" name="short_description" id="short_description" cols="95" ><?php echo $row['short_description']; ?></textarea></td>
           
			</td>
		</tr>
        <tr>
			<td align="right">Description :&nbsp;&nbsp;</td>
			<td>
                <?php
				include("library/fckeditor/fckeditor.php");
            	$summary = new FCKeditor("description");
				$summary->BasePath = $path;
				$summary->Height=300;
				$summary->Value =  $row['description'];
				$summary->Create();
			?>
            
			</td>
		</tr>
        <tr>
			<td valign="top" width="180" align="right">Main category?</td>
			<td valign="top" width="620">
            <select size="1" name="main_menu">
             <?php
                if($row['status'] == 1)
                {
            ?>
                <option value="1">Yes</option>
                <option value="0">No</option>
            <?php
                }
                else
                {
            ?>
                <option value="0">No</option>
                <option value="1">Yes</option>
            <?php
                }
            ?>
			</select></td>
		</tr>
        <tr>
			<td valign="top" width="180" align="right">Featured Category</td>
			<td valign="top" width="620">
            <select size="1" name="FeaturedCategory">
             <?php
                if($row['status'] == 1)
                {
            ?>
                <option value="1">Yes</option>
                <option value="0">No</option>
            <?php
                }
                else
                {
            ?>
                <option value="0">No</option>
                <option value="1">Yes</option>
            <?php
                }
            ?>
			</select></td>
		</tr>
        <tr>
			<td align="right">Template:&nbsp;&nbsp;</td>
			<td><?php echo $select; ?></td>
		</tr>
        <tr>
			<td align="right">friendly_url :&nbsp;&nbsp;</td>
			<td>
				<input type="text" name="friendly_url" id="friendly_url" value="<?php echo $row['friendly_url']; ?>" />
			</td>
		</tr> 
         <tr>
			<td align="right">Sort Priority :&nbsp;&nbsp;</td>
			<td>
				<input type="text" name="SortPriority" id="SortPriority" value="<?php echo $row['SortPriority']; ?>" />
			</td>
		</tr> 
        <tr>
			<td align="right">Photo link :&nbsp;&nbsp;</td>
			<td>
				<input type="text" name="photo_link" id="photo_link" value="<?php echo $row['photo_link']; ?>" />
			</td>
		</tr> 
        <tr>
			<td align="right">Video Link :&nbsp;&nbsp;</td>
			<td>
				<textarea rows="4" name="embed_youtube" cols="65"><?php echo $row['embed_youtube']; ?></textarea></td>
			</td>
		</tr> 
		<tr>
			<td></td>
			<td colspan="2" align="left" height="40">
				<input type="submit" name="btnUpdate" id="btnUpdate" value="Update"  />
				<input type="reset" value="Reset" />
			</td>
		</tr>
	</table>
</form>
<i>Please fill in these fields (<font style="color: red;">*</font>)</i>
<?php
 }
   else
	{
		echo "<meta http-equiv='refresh' content='0; url=login.php' />";
	}
?>